<?php
namespace app\controller;

use app\exception\HttpResponseException;
use app\service\EntityStatisticsService;
use support\ErrorCode;
use support\Request;
use support\Response;

/**
 * 实体统计
 */
class EntityStatisticsController
{
    /**
     * @var EntityStatisticsService
     */

    private $entityStatisticsService;

    public function __construct()
    {
        $this->entityStatisticsService = new EntityStatisticsService();
    }

    /**
     * 获取新增需求数
     * @param Request $request
     * @return Response
     * @throws HttpResponseException
     */
    public function getEntityAppendDemand(Request $request)
    {
        $param = $request->post()['param'];
        $projectIds = $param['project_ids'];
        $time = $param['range_time'] ?? [0, 0];
        $appendType = $param['standard_append'] ?? '';
        $moduleCode = $param['module_code'] ?? '';
        $stageId = $param['stage_id'] ?? 0;
        $fields = $param['fields'] ?? '';
        $responseFormatType = $param['response_format_type'] ?? '';
        $groupBy = $param['group_by'] ?? '';
        $page = $param['page'] ?? [];
        $this->checkTimeIsOutRange($time);

        $res = $this->entityStatisticsService->getEntityAppendDemand($projectIds, $time, $appendType, $moduleCode, $stageId, $fields, $responseFormatType, $groupBy, $page);
        return json(success_response('', $res));
    }

    /**
     * 获取基本量
     * @param Request $request
     * @return Response
     * @throws HttpResponseException
     */
    public function getBasicDemand(Request $request)
    {
        $param = $request->post()['param'];
        $projectIds = $param['project_ids'];
        $time = $param['range_time'] ?? [0, 0];
        $moduleCode = $param['module_code'] ?? '';
        $fields = $param['fields'] ?? '';
        $responseFormatType = $param['response_format_type'] ?? '';
        $groupBy = $param['group_by'] ?? '';
        $page = $param['page'] ?? [];
        $this->checkTimeIsOutRange($time);

        $res = $this->entityStatisticsService->getBasicDemand($projectIds, $time, $moduleCode, $fields, $responseFormatType, $groupBy, $page);
        return json(success_response('', $res));
    }

    /**
     * 获取需求明细
     * @param Request $request
     * @return Response
     * @throws HttpResponseException
     */
    public function getDemandDetail(Request $request)
    {
        $param = $request->post()['param'];
        $projectIds = $param['project_ids'];
        $time = $param['range_time'] ?? [0, 0];
        $appendType = $param['standard_append'] ?? '';
        $moduleCode = $param['module_code'] ?? '';
        $episodeCode = $param['episode_code'] ?? '';
        $stageId = $param['stage_id'] ?? 0;
        $fields = $param['fields'] ?? '';
        $responseFormatType = $param['response_format_type'] ?? '';
        $groupBy = $param['group_by'] ?? '';
        $this->checkTimeIsOutRange($time);

        $res = $this->entityStatisticsService->getDemandDetail($projectIds, $time, $episodeCode, $appendType, $moduleCode, $stageId, $fields, $responseFormatType, $groupBy);
        return json(success_response('', $res));
    }

    /**
     * 根据需求单id获取资产分类数
     * @param Request $request
     * @return Response
     */
    public function getAssetCategoryCount(Request $request)
    {
        $param = $request->post()['param'];

        $res = $this->entityStatisticsService->getAssetCategoryCount($param['review_feedback_ids']);
        return json(success_response('', $res));
    }

    /**
     * 根据需求单id获取实体id
     * @param Request $request
     * @return Response
     * @throws HttpResponseException
     */
    public function getEntityStatisticsByReviewFeedbackIds(Request $request)
    {
        $param = $request->post()['param'];
        $reviewFeedbackIds = $param['review_feedback_ids'];
        $moduleCode = $param['module_code'] ?? '';
        $responseFormatType = $param['response_format_type'] ?? '';

        $res = $this->entityStatisticsService->getEntityStatisticsByReviewFeedbackIds($reviewFeedbackIds, $moduleCode, $responseFormatType);
        return json(success_response('', $res));
    }

    /**
     * 获取实体预估量
     * @param Request $request
     * @return Response
     * @throws HttpResponseException
     */
    public function getEntityEstimate(Request $request)
    {
        $param = $request->post()['param'];
        $projectIds = $param['project_ids'];
        $time = $param['range_time'] ?? [0, 0];
        $moduleCode = $param['module_code'] ?? '';
        $fields = $param['fields'] ?? '';
        $responseFormatType = $param['response_format_type'] ?? '';
        $groupBy = $param['group_by'] ?? '';
        $page = $param['page'] ?? [];
        $this->checkTimeIsOutRange($time);

        $res = $this->entityStatisticsService->getEntityEstimate($projectIds, $time, $moduleCode, $fields, $responseFormatType, $groupBy, $page);
        return json(success_response('', $res));
    }

    /**
     * 校验时间查询是否超过查询范围（最大只能查一年的数据）
     * @param array $rangeTime
     * @return void
     * @throws HttpResponseException
     */
    private function checkTimeIsOutRange(array $rangeTime)
    {
        list($startTime, $endTime) = $rangeTime;
        if (($endTime - $startTime) > 366 * 24 * 60 * 60) {
            throw_strack_exception('The time range cannot exceed one year!', ErrorCode::TIME_OUT_OF_RANGE);
        }
    }
}